
// if (typeof Object.assign != 'function') {
//     Object.assign = function (target) {
//         'use strict';
//         if (target == null) {
//             throw new TypeError('Cannot convert undefined or null to object');
//         }
//         target = Object(target);
//         for (var index = 1; index < arguments.length; index++) {
//             var source = arguments[index];
//             if (source != null) {
//                 for (var key in source) {
//                     if (Object.prototype.hasOwnProperty.call(source, key)) {
//                         target[key] = source[key];
//                     }
//                 }
//             }
//         }
//         return target;
//     };
// }

// 本地枚举
var uim_local_enum = {
    // 建议使用uim在线版本进行管理，可在动态获取或导出静态js
    respoolStatus: {
        _name: '资源池状态',
        1: '启用', 2: '停用', 3: '删除',
        enabled: 1, suspend: 2, deleted: 3,
    },
    respoolAreaStatus: {
        _name: '资源池可用区状态',
        1: '启用', 2: '停用', 3: '删除',
        enabled: 1, suspend: 2, deleted: 3,
    },
    respoolAreaServerStatus: {
        _name: '资源池可用区服务器状态',
        1: '启用', 2: '停用', 3: '删除',
        enabled: 1, suspend: 2, deleted: 3,
    },
    respoolAreaServerType: {
        _name: '资源池可用区服务器类型',
        0: '扩展服务器', 1: '静态资源节点', 2: '套接字路由', 3: '套接字服务节点', 4: '队列/缓存', 5: '接口消费主控', 6: '接口消费节点', 7: 'http代理',
        ext_server: 0, static_server: 1, socket_router: 2, socket_server: 3, queue_cache: 4, consumer: 5, api_server: 6, http_agent: 7,
    },
    respoolAreaServerDeployType: {
        _name: '资源池可用区服务器部署类型',
        1: '物理机', 2: '祼金属', 3: '云服务器', 4: '容器',
        physical_machine: 1, bare_metal: 2, cloud_server: 3, container: 4,
    },
    respoolAreaServerNetworkStatus: {
        _name: '资源池可用区服务器网络状态',
        1: '启用', 2: '停用', 3: '删除',
        enabled: 1, suspend: 2, deleted: 3,
    },
    respoolAreaServerNetworkType: {
        _name: '资源池可用区服务器网络类型',
        0: '本地回环', 1: '本机（物理机/虚拟机/容器）', 2: '局域网', 3: '内部网/区域网/城域网', 4: '互联网/外部网',
        self: 0, local: 1, lan: 2, zone: 3, outer: 4,
    },
    projectStatus: {
        _name: '项目状态',
        1: '启用', 2: '禁用', 3: '删除',
        enabled: 1, disabled: 2, deleted: 3,
    },
    projectAppStatus: {
        _name: '项目应用状态',
        1: '启用', 2: '禁用', 3: '删除',
        enabled: 1, disabled: 2, deleted: 3,
    },
    adminStatus: {
        _name: '管理人员状态',
        1: '启用', 2: '禁用', 3: '删除',
        enabled: 1, disabled: 2, deleted: 3,
    },
};

// 本地配置
var uim_local_config = {
    app_name: 'Uim部署控制台',
    app_version: '1.0',
    view_src: '/view', // 视图地址
    cache_view: true, // 是否缓存视图
    tmp_view_server: '/', // 临时视图服务器（用于IE等非现代浏览器时使用http转现）
    // 除以上特殊用途的配置外，可在下方继续添加自定义配置
    // "键": "值"
    socket_server: localStorage.socket_server || 'ws://console.demo.open-src.uim.site:5098'
};

// 本地会话
var uim_local_session = {
    // id: '', // 会话编号
    // account_id: '', // 用户账户编号
    power: {
        "_childs": ["page/dashboard", "page/description", "page/updateLog", "respool", "area", "server", "project", "app", 'relation', "system"],
        "page/dashboard": {
            "_childs": [],
            "_data": {
                "power_id": -1, "parent_id": 0, "power_code": "page/dashboard", "power_name": "主面板",
                "ext_data": { "iconfont": "tachometer-alt", "menu": { "has_child": false, "url": "" } }
            },
        },
        "page/description": {
            "_childs": [],
            "_data": {
                "power_id": -1, "parent_id": 0, "power_code": "page/description", "power_name": "系统说明",
                "ext_data": { "iconfont": "info-circle", "menu": { "has_child": false, "url": "" } }
            },
        },
        "page/updateLog": {
            "_childs": [],
            "_data": {
                "power_id": -1, "parent_id": 0, "power_code": "page/updateLog", "power_name": "更新日志",
                "ext_data": { "iconfont": "edit", "menu": { "has_child": false, "url": "" } }
            },
        },
        "project": {
            "_childs": ["list"],
            "_data": {
                "power_id": 1, "parent_id": 0, "power_code": "project", "power_name": "项目管理",
                "ext_data": { "iconfont": "pennant", "menu": { "has_child": true, "url": "" }, category: '部署配置' }
            },
            "list": {
                "_childs": [],
                "_data": {
                    "power_id": 25, "parent_id": 1, "power_code": "list", "power_name": "项目列表",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            }
        }, "app": {
            "_childs": ["list"],
            "_data": {
                "power_id": 1, "parent_id": 0, "power_code": "app", "power_name": "应用管理",
                "ext_data": { "iconfont": "puzzle-piece", "menu": { "has_child": true, "url": "" }, category: '部署配置' }
            },

            "list": {
                "_childs": [],
                "_data": {
                    "power_id": 25, "parent_id": 1, "power_code": "list", "power_name": "应用列表",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            }
        },
        "relation": {
            "_childs": ['project-router', 'project-queue', 'project-consumer', 'project-api', 'router-gateway', 'queue-consumer', 'consumer-api'],
            "_data": {
                "power_id": 25, "parent_id": 1, "power_code": "relation", "power_name": "挂载关系",
                "ext_data": { "iconfont": "magnet", "menu": { "has_child": true }, category: '部署配置' }
            },
            "project-router": {
                "_childs": [],
                "_data": {
                    "power_id": 25, "parent_id": 1, "power_code": "router", "power_name": "项目-路由",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            },
            "project-queue": {
                "_childs": [],
                "_data": {
                    "power_id": 25, "parent_id": 1, "power_code": "queue", "power_name": "项目-队列",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            },
            "project-consumer": {
                "_childs": [],
                "_data": {
                    "power_id": 25, "parent_id": 1, "power_code": "consumer", "power_name": "项目-主控",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            },
            "project-api": {
                "_childs": [],
                "_data": {
                    "power_id": 25, "parent_id": 1, "power_code": "api", "power_name": "项目-消费机",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            },
            "router-gateway": {
                "_childs": [],
                "_data": {
                    "power_id": 25, "parent_id": 1, "power_code": "api", "power_name": "路由-网关",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            },
            "queue-consumer": {
                "_childs": [],
                "_data": {
                    "power_id": 25, "parent_id": 1, "power_code": "api", "power_name": "队列-主控",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            },
            "consumer-api": {
                "_childs": [],
                "_data": {
                    "power_id": 25, "parent_id": 1, "power_code": "api", "power_name": "主控-消费机",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            },
        }, "system": {
            "_childs": ["admin_list"],
            "_data": {
                "power_id": 1, "parent_id": 0, "power_code": "system", "power_name": "人员管理",
                "ext_data": { "iconfont": "user-alt", "menu": { "has_child": true, "url": "" }, category: '系统管理' }
            },

            "admin_list": {
                "_childs": [],
                "_data": {
                    "power_id": 25, "parent_id": 1, "power_code": "organize_list", "power_name": "管理员列表",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            }
        }, "area": {
            "_childs": ["list"],
            "_data": {
                "power_id": 2, "parent_id": 0, "power_code": "area", "power_name": "可用区管理",
                "ext_data": { "iconfont": "globe", "menu": { "has_child": true }, category: '基础资源' }
            }, "list": {
                "_childs": [], "_data": {
                    "power_id": 18, "parent_id": 2, "power_code": "list", "power_name": "可用区列表",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            }
        }, "server": {
            "_childs": ["list"],
            "_data": {
                "power_id": 2, "parent_id": 0, "power_code": "server", "power_name": "服务器管理",
                "ext_data": { "iconfont": "server", "menu": { "has_child": true }, category: '基础资源' }
            }, "list": {
                "_childs": [], "_data": {
                    "power_id": 18, "parent_id": 2, "power_code": "list", "power_name": "服务器列表",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            }
        }, "respool": {
            "_childs": ["list"],
            "_data": {
                "power_id": 3, "parent_id": 0, "power_code": "respool", "power_name": "资源池管理",
                "ext_data": { "iconfont": "cloud", "menu": { "has_child": true }, category: '基础资源' }
            },
            // "description": {
            //     "_childs": [], "_data": {
            //         "power_id": 15, "parent_id": 3, "power_code": "description", "power_name": "资源池说明",
            //         "ext_data": { "iconfont": "", "menu": { "has_child": false } }
            //     }
            // },
            "list": {
                "_childs": [], "_data": {
                    "power_id": 19, "parent_id": 3, "power_code": "list", "power_name": "资源池列表",
                    "ext_data": { "iconfont": "", "menu": { "has_child": false } }
                }
            }
        }
    }
    // 除以上特殊用途的会话信息外，可在下方继续添加自定义会话信息
};



// 其他代码

// 临时缓存，目前用于菜单渲染逻辑
var tmp_cache_data
var tmp_cache = function (data) {
    if (tmp_cache_data == data) {
        return false;
    } else {
        tmp_cache_data = data
        return true;
    }
}
uim.view = function (view_name, view_data, iframe_window, success, error) {
    var time1 = new Date().getTime()
    uim.render({
        data: view_data || {},
        template: view_name,
        template_type: 2, // 0默认为模板内容字符串（不支持include），1为标签对象（取innerHTML，其中include时从页面dom.getbyId来取），2为在线模板（include从线上获取）；
        context: window, // 当template_type=1时有效，用于在当前上下文中寻找dom
        // target: 'dom.id',
        done: function (res) {
            console.log('##########render view\n"' + view_name + '" lost time: ', new Date().getTime() - time1)
            // console.log(res)

            // 如果传入了回调函数，则在渲染之后，视图跳转之前执行
            if (res.error) {
                error && error(res.error)
                return console.error('uim.view error.', res)
            }
            success && success()

            res.content = res.content.replace('>', '><script>var uim=top.uim,view_name="' + view_name + '";if (uim.init_param.report_error) window.onerror = uim.on_error;</script>');

            var set_window_iframe_content = function (err, href) {
                iframe_window = iframe_window || frames[0];

                if (err) {
                    iframe_window.document.write(err);
                } else {
                    iframe_window.location = href;
                    history.replaceState({}, '', '#' + view_name);
                    console.log('%crender view: ' + view_name, 'color: #00bfff')
                }
            }
            if ('ActiveXObject' in window) {
                // 如果是ie浏览器，并且设置了
                if (uim.config.tmp_view_server) {

                    var xmlhttp = new XMLHttpRequest();
                    xmlhttp.open('POST', uim.config.tmp_view_server + 'set-tmp-view');
                    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
                    var formData = new FormData();
                    formData.append('html', res.content);
                    xmlhttp.onload = function () {
                        var data;
                        try {
                            data = JSON.parse(this.responseText);
                        } catch (ex) {
                            set_window_iframe_content('临时视图服务器返回失败')
                        }
                        if (data)
                            set_window_iframe_content(null, uim.config.tmp_view_server + 'get-tmp-view?page_id=' + data.page_id);
                        else
                            set_window_iframe_content('临时视图服务器转储失败')
                    };
                    xmlhttp.onerror = function (err) {
                        set_window_iframe_content('连接临时视图服务器失败（网络错误）')
                    }
                    xmlhttp.send(formData);
                } else {
                    set_window_iframe_content('当前浏览器不支持本地渲染视图！')
                }
            } else {
                var html_blob = new Blob([res.content], { type: 'text/html,charset=UTF-8' })
                var html_blob_url = window.URL.createObjectURL(html_blob);
                set_window_iframe_content(null, html_blob_url);
            }
        }
    })
}


// 执行初始化
uim.init({
    // 部署应用：必须，字符串类型（用于校验）
    deploy_app_id: 'U3RiWkJof2u4TwGtpx6V25bac3LCtNuDnZz9', // 将console_main.js生成的应用编号粘贴到此处
    // 服务器地址：为空则不连接服务器（需自行校验应用和会话）
    server_host: '',

    // 枚举（采用本地js定义时传入，否则不传）
    enum: uim_local_enum,
    // 配置（采用本地js定义时传入，否则不传）
    config: uim_local_config,
    // 会话（服务器地址为空时传入）
    session: uim_local_session,

    // 成功回调
    done: function () {
        console.log('uim初始成功');
        window.uim_readly();
    },
    // 失败回调
    fail: function (err) {
        console.log('uim初始失败', err);
    }
});